Can we use Python/terraform scripting to create incidents in Pagerduty?

Hi

I have an automation requirement where we need to create incidents in PagerDuty using scripts. Is there any documentation regarding the same?

Which scripting language is recommended to do so?
I was planning for Python or Terraform. Any suggestions on this.

Thanks
Swati

Yes, you can use any scripting language to create PagerDuty Incidents via the REST API directly or via the Events API via an event/alert.

A quick tip - visit the REST API documentation for “Create an Incident” here: https://developer.pagerduty.com/api-reference/b3A6Mjc0ODE0MA-create-an-incident

Look to the middle right side of the page for the “Request Example” and select Python (or other languages) and you’ll get a simple example to use.

Full example:

import requests

url = "https://api.pagerduty.com/incidents"

payload = {"incident": {
        "type": "incident",
        "title": "The server is on fire.",
        "service": {
            "id": "PWIXJZS",
            "type": "service_reference"
        },
        "priority": {
            "id": "P53ZZH5",
            "type": "priority_reference"
        },
        "urgency": "high",
        "incident_key": "baf7cf21b1da41b4b0221008339ff357",
        "body": {
            "type": "incident_body",
            "details": "A disk is getting full on this machine. You should investigate what is causing the disk to fill, and ensure that there is an automated process in place for ensuring data is rotated (eg. logs should have logrotate around them). If data is expected to stay on this disk forever, you should start planning to scale up to a larger disk."
        },
        "escalation_policy": {
            "id": "PT20YPA",
            "type": "escalation_policy_reference"
        }
    }}
headers = {
    "Content-Type": "application/json",
    "Accept": "application/vnd.pagerduty+json;version=2",
    "From": "",
    "Authorization": "Token token=YOUR TOKEN HERE"
}

response = requests.request("POST", url, json=payload, headers=headers)

print(response.text)

For python, I also recommend using the PDPYRAS library here: https://pagerduty.github.io/pdpyras/

Thanks a lot Doug !!
I will work on automation scripts using your suggestions and will post any further queries , if required.

Regards
Swati